package J5_28;

import java.util.ArrayList;
import java.util.Scanner;

public class test {
    public class ListNode {
    int val;
    ListNode next = null;
    public ListNode(int val) {
      this.val = val;
    }
  }
    public ListNode mergeKLists (ArrayList<ListNode> lists) {
        // write code here
        ListNode res = new ListNode(0);
        ListNode cur = res;
        while (true) {
            int min = 1001;
            int pos = -1;
            for (int i = 0; i < lists.size(); i++) {
                if (lists.get(i) != null && lists.get(i).val < min) {
                    min = lists.get(i).val;
                    pos = i;
                }
            }
            if (pos == -1) {
                break;
            }
            cur.next = lists.get(pos);
            cur = cur.next;
            lists.set(pos, lists.get(pos).next);
        }
        return res.next;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt();
        int[][] arr = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j] = in.nextInt();
            }
        }
        
    }
}
